package com.shop.loan.service.impl;

import com.shop.loan.mapper.EruptUserChannelByMybatisMapper;
import com.shop.loan.model.Channel;
import com.shop.loan.service.ChannelService;
import org.springframework.stereotype.Service;
import xyz.erupt.jpa.dao.EruptDao;
import xyz.erupt.upms.model.EruptRole;
import xyz.erupt.upms.model.EruptUser;
import xyz.erupt.upms.service.EruptUserService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class ChannelServiceImpl implements ChannelService {

    @Resource
    EruptDao eruptDao;

    @Resource
    EruptUserService eruptUserService;

    @Resource
    EruptUserChannelByMybatisMapper eruptUserChannelByMybatisMapper;

    public Channel getChannelByCode(String code) {
        Channel channel = eruptDao.queryEntity(Channel.class, "code = :code and is_delete = false", new HashMap<String, Object>(1) {
            {
                this.put("code", code);
            }
        });
        return channel;
    }


    @Override
    public List<Long> getChannelIds() {
        EruptUser eruptUser = eruptUserService.getCurrentEruptUser();
        Boolean isChannel = false;
        for (EruptRole role : eruptUser.getRoles()) {
            if ("channel".trim().equals(role.getCode())) {
                isChannel = true;
            }
        }
        if (!eruptUserService.getCurrentEruptUser().getIsAdmin() && isChannel) {
            List<Long> channelIds = eruptUserChannelByMybatisMapper.selectChannelByUser(eruptUser.getId());
            if (channelIds == null) {
                channelIds = new ArrayList<>();
                channelIds.add(-1l);
            }
           return channelIds;
        }
        return null;
    }
}
